/**==============================================
 * 模块导入区
 */

const { app } = require('photoshop');
const { localFileSystem: fs } = require("uxp").storage;
// const { executeAsModal } = require("photoshop").core;
// const { batchPlay } = require("photoshop").action;
const k = require('./lib/module');
/**======================================
 * 测试区
 */
// let doc = app.activeDocument;
// let layer = doc.layers[0];
//  D:/Own/Art/UI/psd
let test_n = k.kers_test.kers_t_number;

//const selectLayer = k.selectLayer;
//let sl= selectLayer(layer.id);//.then(value=>{return "value"})
//let kl =k.runModalFunction(sl.then(value=>{return "value"}),"Select Layer").then(value=>{return "value"});
//console.log(selectLayer(layer.id).then(value=>{return "value"},reason=>{return "asss"}));
//k.runModalFunction(selectLayer(layer.id),"Select Layer");
//const ctionCommands = async () => { await k.selectLayer(layer.id)}

/**======================================
 * HTML 业务
 */
let path = document.getElementById('text1').value;
document.getElementById('testBtn1').addEventListener('click', () => { test1(); });
// 获取 select 标签的选中值

//let selectedValue = selectElement.value;

/**======================================
* Photoshop 业务
*/


async function test11() {
    try {
        app.activeDocument.length > 1;
    } catch (e) {
        alert("没有打开的文档,请至少打开一个文档");
    }
    let doc = app.activeDocument;
    let layer = doc.layers[0];

    const actionCmd_selectLayer = async () => { await k.selectLayer(layer.id) }
    const actionCmd_openSmartObj = async () => { await k.openSmartObject(layer.id) }

    const actionCmd_convertToSmartObj = async () => { await k.convertToSmartObject(app.activeDocument.id, layer.id) }

    const actionCmd_closeDocument = async () => { await k.closeDocument("yes") }

    //console.log(aa);
    await k.runModalFunction(actionCmd_selectLayer, "selectLayer");
    await k.runModalFunction(actionCmd_openSmartObj, "openSmartObject");

    await k.runModalFunction(actionCmd_convertToSmartObj, "actionCommands_convertToSmartObject");

    await k.runModalFunction(actionCmd_closeDocument, "close document");

}

async function test111() {
    let doc = app.activeDocument;
    let layer = doc.layers[0];
    let url = "D:/Own/Art/UI/psd";//D:/Own/Art/UI/psd/rock-monument1.png
    let url_ = "D:/Own/Art/UI/psd/a20000.png";
    let imgName = "k73-001";

    //const saveFolder = await fs.getFolder();//弹窗选择路径
    const saveFolder = await fs.getEntryWithUrl("file:" + url);//直接给个保存的地址
    //const file_1 = await fs.createEntryWithUrl("file:" + url_, { overwrite: true });//创建
    const entryPng = await saveFolder.createFile(layer.name || imgName, { overwrite: true, });
    // const token = await fs.createSessionToken(saveFolder1);
    // const token_1 = await fs.createSessionToken(file_1);
    //const sv = await fs.getFileForSaving("rock.jpg");

    const actionCmd_test = async () => {

        await doc.saveAs.png(entryPng, { compression: 6 }, true);
        // await doc.saveAs.jpg(sv, { quality: 12 }, true);
    }
    await k.runModalFunction(actionCmd_test);
}

async function exportImage(doc, saveType, entry) {

    let Options = 12;
    if (saveType == "png" || "PNG") {
        return actionCmd_png = async () => {
            if (!Options) {
                await doc.saveAs.png(entry);
            } else if (Options <= 9 & Options >= 0) {
                await doc.saveAs.png(entry, { compression: Options }, true);
            } else {
                alert("png 的压缩数值不在 0~9 之间---默认6");
            }
        }
    } else if (saveType == "jpg" || "JPG" || "JPEG") {
        return actionCmd_jpg = async () => {
            if (Options >= 0 & Options <= 12) {
                await doc.saveAs.jpg(entry, { quality: Options }, true);
            } else {
                alert("jpg 品质不在 0~12 之间---默认12");
            }
        }
    } else if (saveType == "psd" || "PSD") {
        await doc.saveAs.psd(entry);
    } else {
        alert("格式错误/n暂时只有--jpg--png--psd-- 3种格式 ");
    }


}

async function test111() {

    let select = document.getElementById("selectID");
    select.selectedIndex = 0;
    let saveType = select.value;
    let url = document.getElementById('text1').value;

    console.log(saveType); // 输出选中的值

    const doc = app.activeDocument;
    let imgName = app.activeDocument.name;
    //exportImage(doc, saveType)    let imgName
    let saveFolder = await fs.getEntryWithUrl("file:" + url);//直接给个保存的地址
    let entry = await saveFolder.createFile(imgName, { overwrite: true, });//在路径下建立文件 

    await k.runModalFunction(await exportImage(doc, saveType, entry));

}
async function test1() {

        //定义一个变量[document]，用来表示Photoshop的当前文档。
        //const document = app.activeDocument;
        //获取当前选区坐标
        //let bounds = document.selection.bounds;
        //let newbounds= k.getBounds(document);
      
        //调用[document]对象的[crop]方法，来裁切当前文档。
        


     const actionCmd_selectLayer = async () => { await k.cropDocument(app.activeDocument) }
     await k.runModalFunction(actionCmd_selectLayer);

}
/**
 * 切单图逻辑
 * ============
 * 1.选中图层
 * 2.转换智能对象
 * 3.打开智能对象
 * 4.如果带有蒙版--导出尺寸蒙版
 * 5.根据蒙版做选区
 * 6.根据选区裁剪画布
 * 7.导出图标
 * 8.关闭本文档
 * 9.历史记录后退回切图前状态----
 * 
 *  */ 

/**
 * 批量图层导出
 * ===========
 * 1.便利所有图层--包含 图层组 子级↓
 * 2.找到带有标识的 图层 或 图层组
 * 3.选中图层 或 图层组
 * 
 * 
 */





// aveType = async (saveType) => {

//     if (saveType == "PNG") {
//         return await saveType_PNG(entryPng, 6, true);
//     }
//     if (saveType == "JPG") {
//         return await saveType_JPG(entryPng, 12, true);
//     }
//     if (saveType == "PSD") {
//         return await saveType_PSD(entryPng);
//     } else {
//         console.log("其他")
//     }
//     const saveType_PNG = async (entry, Options = 6, asCopy = true) => {

//         await doc.saveAs.png(entry, { compression: Options }, asCopy);

//     }
//     const saveType_JPG = async (entry, Options = 12, asCopy = true) => {

//         await doc.saveAs.png(entry, { quality: Options }, asCopy);

//     }
//     const saveType_PSD = async (entry) => {

//         await doc.saveAs.psd(entry,Options);

//     }
// }

